﻿namespace Sai.Core.Rules
{
    using Sai.Core.Introspection;
    using System.Collections.Generic;
    using System.Linq;

	/// <summary>
	/// Rule used to check fields in a class declaration. 
	/// Checks for obsolete fields and fields removed from second version.
	/// </summary>
	public class CheckTypeFields : IClassRule
	{
		/// <summary>
		/// Validation for this rule. Feeds changes found to type difference object.
		/// </summary>
		/// <param name="was">The old version.</param>
		/// <param name="isNow">The new version.</param>
		/// <param name="typeDifference">The type difference.</param>
        public void Validate(ClassDeclaration was, ClassDeclaration isNow, ITypeDifference typeDifference)
		{
            var isNowFields = isNow.Fields.Where(f => !f.IsConstant);

            var reporter = new FieldDifferenceReporter(typeDifference);

            reporter.ReportOnObsoleteFields(isNowFields, TypeChangeReason.FieldObsolete);

			// look for missing fields
            var wasFields = was.Fields.Where(f => !f.IsConstant);

            reporter.ReportOnRemovedFields(wasFields, isNowFields, TypeChangeReason.FieldRemoved);
            reporter.ReportOnAddedFields(wasFields, isNowFields, TypeChangeReason.FieldAdded);
            reporter.ReportOnChangedFields(wasFields, isNowFields, TypeChangeReason.FieldChanged);
		}
	}
}
